import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)
const login = () => import('../components/pages/login.vue')
const index = () => import('../components/pages/index.vue')

// 管理员页
const manageList = () => import('../components/pages/manageList.vue')
const manageCreate = () => import('../components/pages/manageCreate.vue')

// 用户管理页
const userList = () => import('../components/pages/userList.vue')
const userCreate = () => import('../components/pages/userCreate.vue')

// 通知
const news = () => import('../components/pages/news.vue')
const newsList = () => import('../components/pages/newsList.vue')
const newsCreate = () => import('../components/pages/newsCreate.vue')

// 设备管理
const device = () => import('../components/pages/device.vue')
const deviceList = () => import('../components/pages/deviceList.vue')
const deviceCreate = () => import('../components/pages/deviceCreate.vue')

// 欢迎界面
const welcome = ()=>import('../components/pages/welcome.vue')

import ajax from "../common/js/ajax"
import API from '../common/js/api'
export default new Router({
  routes: [
    {
      path: '/login',
      component: login
    },
    {
      path : '*',
      redirect:'/login'
    },
    {
      path: '/index',
      component: index,
      beforeEnter:(to,from,next)=>{
        ajax(API.findManage,'get',{},d=>{
          // 服务端session已开启
          next()
        },err=>{
          alert(err.data.info)
          next("/login")
        })
      },
      children: [
        {
          path:'',
          component:welcome
        },
        {
          path: 'manageList',
          component: manageList,
          name: '管理员列表',
          beforeEnter: (to, from, next) => {
            // 是用户
            let type = JSON.parse(localStorage.getItem('user')).type;
            if (type != 0) {
              next('/index')
            }
            next()
          }
        },
        {
          path: 'manageCreate',
          component: manageCreate,
          beforeEnter: (to, from, next) => {
            // 是用户
            let type = JSON.parse(localStorage.getItem('user')).type;
            if (type != 0) {
              next('/index')
            }
            next()
          }
        },
        {
          path: 'userList',
          component: userList,
          beforeEnter: (to, from, next) => {
            // 是用户
            let type = JSON.parse(localStorage.getItem('user')).type;
            if (type > 1) {
              next('/index')
            }
            next()
          }
        },
        {
          path: 'userCreate',
          component: userCreate,
          beforeEnter: (to, from, next) => {
            // 是用户
            let type = JSON.parse(localStorage.getItem('user')).type;
            if (type > 1) {
              next('/index')
            }
            next()
          }
        },
        {
          path: 'news',
          component: news,
          children: [
            {
              path: '',
              component: newsList
            },
            {
              path: 'newsCreate',
              component: newsCreate
            }
          ]
        },
        {
          path: 'device',
          component: device,
          children: [
            {
              path: '',
              component: deviceList
            },
            {
              path: 'deviceCreate',
              component: deviceCreate
            }
          ]
        }
      ]
    }
  ]
})
